package pl.vm.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.vm.domain.interfaces.Classification;
import pl.vm.domain.interfaces.Round;

public class SeazonClassification implements Classification {

	private Seazon seazon;
	private Map<VirtualTeam, Double> teamPoints = new HashMap<>();
	private List<VirtualTeam> classification = new ArrayList<VirtualTeam>();
	
	public SeazonClassification(Seazon seazon) {
		this.seazon = seazon;
	}
	
	@Override
	public List<VirtualTeam> getClassification() {
		return classification;
	}

	@Override
	public void calculateTeamPoints() {
		for (VirtualTeam team : seazon.getTeamManager().getVirtualTeams()) {
			double teamPoints = 0;
			for (Round round : seazon.getRoundManager().getRounds()) {
				teamPoints += round.getClassification().getTeamPoints(team);
			}
			this.teamPoints.put(team, teamPoints);
		}
	}

	@Override
	public void calculateClassification() {
		classification.addAll(teamPoints.keySet());
		Collections.sort(classification, new ValueComparator(teamPoints));
	}

	@Override
	public double getTeamPoints(VirtualTeam team) {
		return teamPoints.get(team);
	}

	@Override
	public void addPointsForTeam(VirtualTeam team, double pointsToAdd) {
		double actualPoints = teamPoints.get(team);
		teamPoints.put(team, actualPoints + pointsToAdd);
	}

	@Override
	public void substractPointForTeam(VirtualTeam team, double pointsToSubstract) {
		addPointsForTeam(team, -pointsToSubstract);
	}

}
